package Thread;/*
 *
 *@author:    Xavier
 *@data:      2024 - 01 - 13
 *@time:      10:35
 *
 */

//wait notify
public class Demo15 {
    // 使用这个锁对象来负责加锁， wait， notify
    private static Object locker = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (true) {
                synchronized(locker) {
                    System.out.println("t1 wait start");
                    try {
                        locker.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("t1 wait end");
           }
        });
        t1.start();

        Thread t2 = new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                synchronized (locker) {
                    System.out.println("t2 notify start");
                    locker.notify();
                    System.out.println("t2 notify end");
                }
            }

        });
        t2.start();
    }
}
